package com.koodo.eblog.search.event;

import com.koodo.eblog.search.mq.PostMqIndexMsg;
import com.koodo.eblog.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class PostEsEventListener implements ApplicationListener<PostEsEvent> {

    @Autowired
    SearchService searchService;

    @Override
    public void onApplicationEvent(PostEsEvent postEsEvent) {
        log.info("PostEsEventListener 收到一条消息---------->{}", postEsEvent.getSource());

        switch (postEsEvent.getType()) {
            case PostEsEvent.CREATE_OR_UPDATE:
                searchService.createOrUpdateIndex(new PostMqIndexMsg(postEsEvent.getPostId(), postEsEvent.getType()));
                break;
            case PostEsEvent.REMOVE:
                searchService.removeIndex(new PostMqIndexMsg(postEsEvent.getPostId(), postEsEvent.getType()));
                break;
            default:
                log.error("没找到对应的消息类型，请注意！");
        }
    }

}
